package com.pipayshop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.entity.ShopHotelRecord;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.mapper.HotelShopHotelRecordMapper;
import com.pipayshop.service.HotelShopHotelRecordService;
import com.pipayshop.utils.StringUtil;
import com.pipayshop.utils.ValidationUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 记录酒店入住订单信息表 服务实现类
 * </p>
 *
 * @author zxb
 * @since 2023-08-02
 */
@Service
@Slf4j
public class HotelShopHotelRecordServiceImpl extends ServiceImpl<HotelShopHotelRecordMapper, ShopHotelRecord> implements HotelShopHotelRecordService {

    @Resource
    private HotelShopHotelRecordMapper shopHotelRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createShopHotelRecord(ShopHotelRecord shopHotelRecord) {
        log.info("创建酒店入住记录: {}", shopHotelRecord);

// 1. 校验必填字段
        ValidationUtil.validateString(shopHotelRecord.getRoomId(), "room_type_id_required");
        ValidationUtil.validateString(shopHotelRecord.getUid(), "check_in_account_required");
        ValidationUtil.validateString(shopHotelRecord.getName(), "check_in_name_required");
        ValidationUtil.validateString(shopHotelRecord.getPhone(), "check_in_phone_required");
        ValidationUtil.validateString(shopHotelRecord.getOrderId(), "order_id_required");
        ValidationUtil.validateNotNull(shopHotelRecord.getStartTime(), "check_in_time_required");
        ValidationUtil.validateNotNull(shopHotelRecord.getEndTime(), "check_out_time_required");

        // 4. 生成 recordId
        String recordId = StringUtil.generateShortId();
        shopHotelRecord.setRecordId(recordId);

        // 5. 插入记录
        int insertedRows = shopHotelRecordMapper.insert(shopHotelRecord);
        boolean isSuccess = insertedRows > 0;
        if (!isSuccess) {
            log.error("创建酒店入住记录失败: {}", shopHotelRecord);
            throw new BusinessException("create_hotel_record_failed");
        }
        return isSuccess;
    }



}
